/**
 * @author Johannes Blersch
 * 
 * @since 28.05.11 
 * 
 * Contains the Action listeners for the buttons on the PlayerSettings Window.
 */

package control;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JPanel;

import view.AdvancedSettings;
import view.PlayerPanel;
import view.PlayerSettings;


import control.*;

public class PlayerSettingsControl implements ActionListener {
	
	//association objects
	private PlayerSettings thePlayerSettings;
	private AdvancedSettings advancedSettings;
	private PlayerPanel[] playerPanels = new PlayerPanel[6];
	private IFieldsControl fieldsControl;
	
	//variables
	private int[] playerFigureMatching = {0, 1, -1, -1, -1, -1}; //Stores the figure which the player has selected the index is the player number
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new PlayerSettingsControl(null);
	}
	
	public PlayerSettingsControl(GameControl gc){
		thePlayerSettings = new PlayerSettings(this, gc);
		thePlayerSettings.setVisible(true);
		fieldsControl = gc.getFieldsControl();
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		int figureNumber;
		
		//Action of the newPlayerButton
		if(event.getActionCommand().equals("newPlayer")) {						
			for (int i = 0; i < 6; i++) { //go through all players {
				if (-1 == playerFigureMatching[i]) { //if playerFirueMatching is -1 the player doesn't exist
//Testausgabe
					System.out.println("Found free Player Number: "+i);
					figureNumber = this.nextFigure(i);
					thePlayerSettings.generateNewPlayerPanel(i, figureNumber);
					i = 2000;	//leave loop
				}
			}			
			playerPanels = thePlayerSettings.getPlayerPanels();
		} 
		
		//Action of the deletePlayerButtons
		else if (event.getActionCommand().equals("deletePlayer3")){
			thePlayerSettings.removePlayerPanel(2); //the index in the playerPanels is 1 smaller than the player number
			playerFigureMatching[2] = -1;
			playerPanels = thePlayerSettings.getPlayerPanels();
		} else if (event.getActionCommand().equals("deletePlayer4")){
			thePlayerSettings.removePlayerPanel(3); //the index in the playerPanels is 1 smaller than the player number
			playerFigureMatching[3] = -1;
			playerPanels = thePlayerSettings.getPlayerPanels();
		} else if (event.getActionCommand().equals("deletePlayer5")){
			thePlayerSettings.removePlayerPanel(4); //the index in the playerPanels is 1 smaller than the player number
			playerFigureMatching[4] = -1;
			playerPanels = thePlayerSettings.getPlayerPanels();
		} else if (event.getActionCommand().equals("deletePlayer6")){
			thePlayerSettings.removePlayerPanel(5); //the index in the playerPanels is 1 smaller than the player number
			playerFigureMatching[5] = -1;
			playerPanels = thePlayerSettings.getPlayerPanels();
		} 
		
		//Action of the nextFigureButtons
		else if (event.getActionCommand().equals("nextFigurePlayer1")){
			playerPanels = thePlayerSettings.getPlayerPanels();
			figureNumber = this.nextFigure(0);
			playerPanels[0].displayNewFigureImage(figureNumber); //the index in the playerPanels is 1 smaller than the player number
		} else if (event.getActionCommand().equals("nextFigurePlayer2")){
			playerPanels = thePlayerSettings.getPlayerPanels();
			figureNumber = this.nextFigure(1);
			playerPanels[1].displayNewFigureImage(figureNumber); //the index in the playerPanels is 1 smaller than the player number
		} else if (event.getActionCommand().equals("nextFigurePlayer3")){
			playerPanels = thePlayerSettings.getPlayerPanels();
			figureNumber = this.nextFigure(2);			
			playerPanels[2].displayNewFigureImage(figureNumber); //the index in the playerPanels is 1 smaller than the player number
		} else if (event.getActionCommand().equals("nextFigurePlayer4")){
			playerPanels = thePlayerSettings.getPlayerPanels();
			figureNumber = this.nextFigure(3);			
			playerPanels[3].displayNewFigureImage(figureNumber); //the index in the playerPanels is 1 smaller than the player number
		} else if (event.getActionCommand().equals("nextFigurePlayer5")){
			playerPanels = thePlayerSettings.getPlayerPanels();
			figureNumber = this.nextFigure(4);			
			playerPanels[4].displayNewFigureImage(figureNumber); //the index in the playerPanels is 1 smaller than the player number
		} else if (event.getActionCommand().equals("nextFigurePlayer6")){
			playerPanels = thePlayerSettings.getPlayerPanels();
			figureNumber = this.nextFigure(5);			
			playerPanels[5].displayNewFigureImage(figureNumber); //the index in the playerPanels is 1 smaller than the player number
		} 
		
		//Action of the lasFigureButtons
		else if (event.getActionCommand().equals("lastFigurePlayer1")){
			playerPanels = thePlayerSettings.getPlayerPanels();
			figureNumber = this.lastFigure(0);
			playerPanels[0].displayNewFigureImage(figureNumber); //the index in the playerPanels is 1 smaller than the player number
		} else if (event.getActionCommand().equals("lastFigurePlayer2")){
			playerPanels = thePlayerSettings.getPlayerPanels();
			figureNumber = this.lastFigure(1);
			playerPanels[1].displayNewFigureImage(figureNumber); //the index in the playerPanels is 1 smaller than the player number
		} else if (event.getActionCommand().equals("lastFigurePlayer3")){
			playerPanels = thePlayerSettings.getPlayerPanels();
			figureNumber = this.lastFigure(2);			
			playerPanels[2].displayNewFigureImage(figureNumber); //the index in the playerPanels is 1 smaller than the player number
		} else if (event.getActionCommand().equals("lastFigurePlayer4")){
			playerPanels = thePlayerSettings.getPlayerPanels();
			figureNumber = this.lastFigure(3);			
			playerPanels[3].displayNewFigureImage(figureNumber); //the index in the playerPanels is 1 smaller than the player number
		} else if (event.getActionCommand().equals("lastFigurePlayer5")){
			playerPanels = thePlayerSettings.getPlayerPanels();
			figureNumber = this.lastFigure(4);			
			playerPanels[4].displayNewFigureImage(figureNumber); //the index in the playerPanels is 1 smaller than the player number
		} else if (event.getActionCommand().equals("lastFigurePlayer6")){
			playerPanels = thePlayerSettings.getPlayerPanels();
			figureNumber = this.lastFigure(5);			
			playerPanels[5].displayNewFigureImage(figureNumber); //the index in the playerPanels is 1 smaller than the player number
		}
		
		//actions of the standard menu buttons
		else if (event.getActionCommand().equals("advancedSettings")){
			advancedSettings = new AdvancedSettings(fieldsControl);
		} 
	}
	
	/**
	 * Sets the next Figure for the chosen player and controls that no image is chosen two times
	 * 
	 * @param playerNumber Number of the chosen player between 0 and 5.
	 * @return Number of the new displayed Image.
	 */
	public int lastFigure(int playerNumber) {
		int actualFigure, i;
		boolean newImageAlreadyChosen = true;
		
		actualFigure = playerFigureMatching[playerNumber];
		
		if (actualFigure <= 0) {
			actualFigure = 5;
		} else {
			actualFigure--;
		}	
		
		while (true == newImageAlreadyChosen) {			
			newImageAlreadyChosen = false;
			for (i = 0; i < 6; i++) { //go through all players
				if (i != playerNumber) { //except the player which wants to change his image
					if (playerFigureMatching[i] == actualFigure) { //check if the actual figure figure is used by another player
						if (actualFigure <= 0) {
							actualFigure = 5;
						} else {
							actualFigure--;
						}
						newImageAlreadyChosen = true;
					}
				}
			}
		}
		
		playerFigureMatching[playerNumber] = actualFigure;
		
//testausgabe		
		for (int hilf = 0; hilf < 6; hilf++)
		{
			System.out.print("Figure Player"+hilf);
			System.out.println(playerFigureMatching[hilf]);
		}
		
		System.out.println("Player"+playerNumber+": Figure: "+actualFigure);
		return actualFigure;
	}
	
	
	/**
	 * Sets the next Figure for the chosen player and controls that no image is chosen two times
	 * 
	 * @param playerNumber Number of the chosen player between 0 and 5.
	 * @return Number of the new displayed Image.
	 */
	public int nextFigure(int playerNumber) {
		int actualFigure, i;
		boolean newImageAlreadyChosen = true;
		
		actualFigure = playerFigureMatching[playerNumber];
		
		if (actualFigure < 5) {
			actualFigure++;
		} else {
			actualFigure = 0;
		}	
		
		while (true == newImageAlreadyChosen) {			
			newImageAlreadyChosen = false;
			for (i = 0; i < 6; i++) { //go through all players
				if (i != playerNumber) { //except the player which wants to change his image
					if (playerFigureMatching[i] == actualFigure) { //check if the actual figure figure is used by another player
						if (actualFigure < 5) {
							actualFigure++;
						} else {
							actualFigure = 0;
						}
						newImageAlreadyChosen = true;
					}
				}
			}
		}
		
		playerFigureMatching[playerNumber] = actualFigure;
		
//testausgabe		
		for (int hilf = 0; hilf < 6; hilf++)
		{
			System.out.print("Figure Player"+hilf);
			System.out.println(playerFigureMatching[hilf]);
		}
		
		System.out.println("Player"+playerNumber+": Figure: "+actualFigure);
		return actualFigure;
	}
	
	/**
	 * @return A Collection of the Panels with info about the players attending the game
	 */
	public PlayerPanel[] getPlayerPanels()
	{
		return this.thePlayerSettings.getPlayerPanels();
	}
	
	public void dispose()
	{
		this.thePlayerSettings.dispose();
	}

}
